<?php

namespace app\admin\controller\user;

use app\common\controller\AdminController;
use app\admin\service\annotation\ControllerAnnotation;
use app\admin\service\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="user_withdraw")
 */
class Withdraw extends AdminController
{

    use \app\admin\traits\Curd;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->model = new \app\admin\model\user\UserWithdraw();
        
        $this->assign('select_list_check_status', $this->model::SELECT_LIST_CHECK_STATUS, true);

        $this->assign('select_list_cash_status', $this->model::SELECT_LIST_CASH_STATUS, true);

        $this->assign('select_list_account_type', $this->model::SELECT_LIST_ACCOUNT_TYPE, true);
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where, $excludes, $request_options, $group) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->group($group)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->group($group)
                ->select();
            foreach($list as &$v){
                if($v->cash_status == 3){
                    $v->cash_status == '&nbsp;';
                }
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        $types = input('types');
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            try {
                if($types == 1){
                    $post['check_time'] = time();
                    if($post['check_status'] == 2){
                        $post['cash_status'] = 3;
                        //审核拒绝返还余额
                        \app\admin\model\MoneyLog::changeMoney($row->user_id,0,$row->cash_fee,5,'提现审核拒绝返还余额',$row->order_sn);
                    }
                }
                if($types == 2){
                    $post['cash_time'] = time();
                }
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:' . $e->getMessage());
            }
            $save ? $this->success('请求成功') : $this->error('请求失败');
        }
        $this->assign('row', $row);
        $this->assign('types', $types);
        return $this->fetch();
    }
    
}